package com.karry.util;

import com.karry.pojo.Question;

import java.util.List;
import java.util.Queue;

public class TextMatchUtil {


    /**
     *  需要剔除的格式
     */
    private static final char[] cnSignArr = "。？！，、；：“” ‘’「」『』（）〔〕【】—﹏…～·《》〈〉".toCharArray();
    private static final char[] enSignArr = "`!@#$%^&*()_+~-=[]\\;',./{}|:\"<>?".toCharArray();

    private List<Question> list;
    private String text;

    private int[] indicator;

    public TextMatchUtil(List<Question> list, String text){
        this.list = list;
        this.text = text;
        //处理文本
        this.text = clearNonWordChar(text);


    }


    //清理格式
    private String clearNonWordChar(String str) {
        for(int i=0;i<cnSignArr.length;i++)
            str = str.replace(""+cnSignArr[i], "");
        for(int i=0;i<enSignArr.length;i++)
            str = str.replace(""+enSignArr[i], "");
        return str;
    }

    public List<Question> doProcess() {

        char[] matchArray = this.text.toCharArray();

        System.out.println("list.size:" + list.size());

        for (int index = 0; index < this.list.size(); index++) {

            double matchingRate, accuracyRate;

            Question question = list.get(index);


            indicator = new int[matchArray.length];

            char[] chars = question.getText().toCharArray();

            int index_ = 0;
            double matchCount = 0;
            for(int i = 0;i < matchArray.length;i++) {
                for (int j = index_; j < chars.length; j++) {
                    if(chars[j] == matchArray[i]) {
                        index_++;
                        indicator[i] = 1;
                        matchCount++;
                        break;
                    }
                }
            }

            for(int i=0;i<matchArray.length;i++)
            {
                for (int j = 0; j < chars.length; j++)
                {
                    if(matchArray[i] == chars[j])
                    {
                        index_++;
                        indicator[i] = 1;
                        matchCount++;
                        break;
                    }
                }
            }

            matchingRate = matchCount/(chars.length*2)*100;
            accuracyRate = matchingRate * matchCount / (matchArray.length);

            System.out.println("匹配值：" + (matchingRate+accuracyRate));
            if (matchingRate + accuracyRate < 50){
                System.out.println("remove_index:" + index);
                list.remove(index);
            }
        }

        return list;
    }


}
